package com.sanshi.project.procure.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.ProcureWarehouseDTO;
import com.sanshi.project.admin.api.entity.CodeRuleEntity;
import com.sanshi.project.admin.api.entity.ProcureWarehouseEntity;
import com.sanshi.project.assets.service.CodeRuleService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import com.sanshi.project.procure.service.ProcureWarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库管理
 *
 * @author nikon
 * @date 2023-12-08 10:54:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/procureWarehouse")
@Tag(description = "procureWarehouse", name = "仓库管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProcureWarehouseController {

    private final ProcureWarehouseService procureWarehouseService;
    private final CodeRuleService codeRuleService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param procureWarehouse 仓库管理
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('procure_procureWarehouse_view')")
    public R getProcureWarehousePage(@ParameterObject Page page, @ParameterObject ProcureWarehouseDTO procureWarehouse) {
        LambdaQueryWrapper<ProcureWarehouseEntity> wrapper = Wrappers.lambdaQuery();
        // 仓库编号
        wrapper.like(StrUtil.isNotBlank(procureWarehouse.getCode()), ProcureWarehouseEntity::getCode, procureWarehouse.getCode());
        // 仓库名称
        wrapper.like(StrUtil.isNotBlank(procureWarehouse.getName()), ProcureWarehouseEntity::getName, procureWarehouse.getName());
        // 仓库类型
        wrapper.in(ArrayUtil.isNotEmpty(procureWarehouse.getType()), ProcureWarehouseEntity::getType, procureWarehouse.getType());
        // 部门
        wrapper.in(ArrayUtil.isNotEmpty(procureWarehouse.getDeptId()), ProcureWarehouseEntity::getDeptId, procureWarehouse.getDeptId());
        wrapper.orderByDesc(ProcureWarehouseEntity::getUpdateTime);
        return R.ok(procureWarehouseService.page(page, wrapper));
    }


    /**
     * 通过id查询仓库管理
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('procure_procureWarehouse_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(procureWarehouseService.getById(id));
    }

    /**
     * 新增仓库管理
     *
     * @param procureWarehouse 仓库管理
     * @return R
     */
    @Operation(summary = "新增仓库管理", description = "新增仓库管理")
    @SysLog("新增仓库管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('procure_procureWarehouse_add')")
    public R save(@RequestBody ProcureWarehouseEntity procureWarehouse) {
        CodeRuleEntity codeRuleEntity = new CodeRuleEntity();
        codeRuleEntity.setType("500");
        R<String> resultCode = codeRuleService.generateCode(codeRuleEntity);
        if (resultCode.isOk()) {
            // 赋予仓库编码
            procureWarehouse.setCode(resultCode.getData());
        } else {
            return resultCode;
        }
        return R.ok(procureWarehouseService.save(procureWarehouse));
    }

    /**
     * 修改仓库管理
     *
     * @param procureWarehouse 仓库管理
     * @return R
     */
    @Operation(summary = "修改仓库管理", description = "修改仓库管理")
    @SysLog("修改仓库管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('procure_procureWarehouse_edit')")
    public R updateById(@RequestBody ProcureWarehouseEntity procureWarehouse) {
        return R.ok(procureWarehouseService.updateById(procureWarehouse));
    }

    /**
     * 通过id删除仓库管理
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除仓库管理", description = "通过id删除仓库管理")
    @SysLog("通过id删除仓库管理")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('procure_procureWarehouse_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(procureWarehouseService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     *
     * @param procureWarehouse 查询条件
     * @param ids              导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('procure_procureWarehouse_export')")
    public List<ProcureWarehouseEntity> export(ProcureWarehouseEntity procureWarehouse, Long[] ids) {
        return procureWarehouseService.list(Wrappers.lambdaQuery(procureWarehouse).in(ArrayUtil.isNotEmpty(ids), ProcureWarehouseEntity::getId, ids));
    }

    /**
     * 仓库管理全量查询
     *
     * @param procureWarehouse 仓库管理
     * @return
     */
    @Operation(summary = "仓库管理全量查询", description = "仓库管理全量查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('procure_procureWarehouse_view')")
    public R getProcureWarehouseList(@ParameterObject ProcureWarehouseDTO procureWarehouse) {
        LambdaQueryWrapper<ProcureWarehouseEntity> wrapper = Wrappers.lambdaQuery();
        // 仓库编号
        wrapper.like(StrUtil.isNotBlank(procureWarehouse.getCode()), ProcureWarehouseEntity::getCode, procureWarehouse.getCode());
        // 仓库名称
        wrapper.like(StrUtil.isNotBlank(procureWarehouse.getName()), ProcureWarehouseEntity::getName, procureWarehouse.getName());
        // 仓库类型
        wrapper.in(ArrayUtil.isNotEmpty(procureWarehouse.getType()), ProcureWarehouseEntity::getType, procureWarehouse.getType());
        // 部门
        wrapper.in(ArrayUtil.isNotEmpty(procureWarehouse.getDeptId()), ProcureWarehouseEntity::getDeptId, procureWarehouse.getDeptId());
        return R.ok(procureWarehouseService.list(wrapper));
    }
}